package org.six11.olive3.picturephone;

import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;

import org.six11.olive3.client.OliveState;
import org.six11.olive3.net.MessageNames;
import org.six11.olive3.net.OliveNetworkTransmitter;
import org.six11.olive3.util.MultiState;
import org.six11.util2.Debug;
import org.six11.util2.gui.ApplicationFrame;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 * 
 */
public class PicturephoneApplet extends JApplet {

  static {
    Debug.useColor = false;
  }

  public PicturephoneApplet() {
    super();
  }

  public void start() {
    bug("start");
  }

  public void stop() {
    bug("stop");
  }

  public void init() {
    // OliveState state;
    MultiState programState = new MultiState();
    OliveNetworkTransmitter xmit;
    String urlString = getCodeBase().toExternalForm() + "pp";

    if (urlString.length() == 0) {
      bug("Using DEFAULT urlString of localhost:8080/picturephone/pp");
      urlString = "http://localhost:8080/picturephone/pp";
    }
    bug("I am connectng to: " + urlString + "'");
    try {
      String uid = getParameter("userID");
      if (uid != null && uid.length() > 0) {
        programState.setValue(MessageNames.PARAM_USER_ID, uid);
      }
      xmit = new OliveNetworkTransmitter(programState, new URL(urlString));
      if (uid == null || uid.length() == 0) {
        xmit.addToMessage("login ()", "");
      }
      PicturephoneUI ui = new PicturephoneUI(xmit, programState);
      add(ui);

    } catch (MalformedURLException ex) {
      ex.printStackTrace();
    }
  }

  public void destroy() {
    bug("destroy");
  }

  // @SuppressWarnings("unused")
  private void bug(final String what) {
    Debug.out("PicturephoneApplet ", what);
  }

}
